package fr.cephb.locustree;

import java.io.Serializable;
import java.util.Map;

import com.sleepycat.je.DatabaseEntry;

import fr.cephb.berkeleydb.binding.GZipBinding;
import fr.cephb.berkeleydb.binding.JSONBinding;
import fr.cephb.json.JSONable;

public abstract class WithMetadata
	implements JSONable,Serializable
	{
	private static final long serialVersionUID = 1L;
	private byte _metaData[]=null;
	private Map<String,Object> _json=null;
	 
	protected WithMetadata()
		 {
		 
		 }
	
	public void setJSON(Map<String, Object> json)
		{
		 this._json=json;
		 this._metaData=null;
		}
	
	@SuppressWarnings("unchecked")
	public Map<String, Object> getJSON()
		{
		 if(this._metaData!=null)
			 {
			 GZipBinding<Object> objectBinding=new GZipBinding<Object>(new JSONBinding());
			 DatabaseEntry entry=new DatabaseEntry(this._metaData);
			 this._json= (Map)objectBinding.entryToObject(entry);
			 this._metaData=null;
			 }
		 return this._json;
		}
	
	
	protected byte[] getMetaData()
		{
		 if(this._json!=null)
			 {
			 GZipBinding<Object> objectBinding=new GZipBinding<Object>(new JSONBinding());
			 DatabaseEntry entry=new DatabaseEntry();
			 objectBinding.objectToEntry(this._json, entry);
			 this._metaData=entry.getData();
			 this._json=null;
			 }
		 return this._metaData;
		}
	
	protected void setMetaData(byte[] metaData)
		{
		_metaData = metaData;
		}
	
	@Override
	public abstract boolean equals(Object obj);

	@Override
	public abstract int hashCode();
	 
	 @Override
	public String toString()
		{
		return toJSON();
		}
	}
